<?php
/**
 * Created by PhpStorm.
 * User: yanghe
 * Date: 2016/5/10
 * Time: 16:52
 */
namespace MyClass\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
use MyClass\Admin\ImageBase;
class ArticleBase
{
    private $article_id;

    public static function addArticle($data)
    {
        $data["article_create_time"]=date("Y-m-d H:i:s");
        $data["article_update_time"]=date("Y-m-d H:i:s");
        $res=DB::table("article")->insertGetId($data);
        if($res)
        {
            $img_data['content']=$data["article_detail"];
            $img_data['title']=$data['article_title'];
            $img_data['id']=$res;
            ImageBase::putImageByUE($img_data);
            return true;
        }
        else
        {
            return false;
        }
    }


    /**
     * @return mixed首页
     */
    public static function allArticle(){
        $data=DB::table('article')->get();
        return $data;
    }

    public static function sAllArticle()
    {
        $class=Request::input("class",null);
        $sort=Request::input("sort","article_id");
        $key=Request::input("key",null);

        $data["sort"] = $sort;
        $data["class"] = $class;
        $data["key"]=$key;

        if($class !=null)
        {
            $data["inputData"]=DB::table("article")
                ->join("article_class","article_class","=","class_id")
                ->where("article_class","=",$class)
                ->where("article_title","like","%".$key."%")
                ->orderBy($sort,"desc")
                ->paginate(5);
            $data["fen"]=$data["inputData"]->appends(['sort'=>$sort,'class'=>$class,'key'=>$key])->render();
        }
        else
        {
            $data["inputData"]=DB::table("article")
                ->join("article_class","article_class","=","class_id")
                ->where("article_title","like","%".$key."%")
                ->orderBy($sort,"desc")
                ->paginate(5);
            $data["fen"]=$data["inputData"]->appends(['sort'=>$sort,'key'=>$key])->render();
        }
        $data["nowPage"]=$data["inputData"]->currentPage();
        $data["allPage"]=$data["inputData"]->lastPage();
        if($data)
        {
            return $data;
        }
        else
        {
            return false;
        }
    }
    public function __construct($article_id)
    {
        $this->article_id=$article_id;
    }

    public function sArticle()
    {
        $data=DB::table("article")
            ->join("article_class","article_class","=","class_id")
            ->where("article_id","=",$this->article_id)
            ->get();
        DB::table("article")->where("article_id","=",$this->article_id)->increment('article_click');
        if($data)
        {
            return $data;
        }
        else
        {
            return false;
        }
    }
    public function uArticle($data)
    {
        $data["article_update_time"]=date("Y-m-d H:i:s");
        $res=DB::table("article")->where("article_id","=",$this->article_id)->update($data);
        if($res)
        {
            $img_data['content']=$data["article_detail"];
            $img_data['id']=$this->article_id;
            $img_data['title']=$data['article_title'];
            ImageBase::uImage($img_data);
            return true;
        }
        else
        {
            return false;
        }
    }
    public function dArticle()
    {
        $data=DB::table("image")->where("image_article","=",$this->article_id)->get();
        $res=DB::table("article")->where("article_id","=",$this->article_id)->delete();
        if($res)
        {
            ImageBase::dAllImage($data);
            return true;
        }
        else
        {
            return false;
        }
    }
    public static function sArticleByClassId($class_id)
    {
        $data=DB::table("article")->where("article_class","=",$class_id)
            ->get();
        if($data)
        {
            return $data;
        }
        else
        {
            return false;
        }
    }


}